package au.edu.qut.bpm.genbinder.models;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AbstractLog {
	private String id;

	private List<Case> cases;
	private Set<Resource> resources;
	private Set<Task> tasks;
	private Map<Task, Set<Set<Resource>>> resourceSetCanDoTask;
	private Set<Activity> acts = new HashSet<Activity>();

	public AbstractLog(String id) {
		this.id = id;
		this.cases = new ArrayList<Case>();
		this.resources = new HashSet<Resource>();
		this.tasks = new HashSet<Task>();
		this.resourceSetCanDoTask = new HashMap<Task, Set<Set<Resource>>>();
	}

	public String getId() {
		return id;
	}

	public Set<Activity> getActivities() {
		for (Case c : getCases())
			acts.addAll(c.getActivities());
		return acts;
	}
	
	public Activity getActivity(Activity act) {
		Set<Activity> allActs = getActivities();
		for (Activity a : allActs) {
			if (a.equals(act)) {
				return a;
			}
		}
		return null;
	}

	public List<Case> getCases() {
		return cases;
	}

	public Set<Resource> getResources() {
		return resources;
	}

	public Set<Task> getTasks() {
		return tasks;
	}

	public Map<Task, Set<Set<Resource>>> getTaskResourceSetMap() {
		return resourceSetCanDoTask;
	}
	
	public void setTaskResourceSetMap(Map<Task, Set<Set<Resource>>> mpCanDo) {
		resourceSetCanDoTask.putAll(mpCanDo);
	}

	public boolean isAllowedResourceSetFor(Task task, Set<Resource> res) {
		if (!resourceSetCanDoTask.containsKey(task))
			return false;
		return resourceSetCanDoTask.get(task).contains(res);
	}

	public long getFirstArrival() {
		long f = -1;
		for (Case c : cases)
			if (c.getArrival() < f || f < 0)
				f = c.getArrival();
		return f;
	}

	@Override
	public String toString() {
		return id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractLog other = (AbstractLog) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
